#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e6 + 5;

int n, m;
int w[N];
vector<int> ch[N];
int vis[N], r1, r2;
ll dp[N][2], ans;
void find(int u, int root) {
  vis[u] = 1;
  for (int v : ch[u]) {
    if (v == root) {
      r1 = u, r2 = v;
      return;
    }
    if (!vis[v]) find(v, root);
  }
}
ll dfs(int u, int root) {
  dp[u][1] = w[u];
  dp[u][0] = 0;
  for (int v : ch[u]) {
    if (v == root) continue;
    dfs(v, root);
    dp[u][1] += dp[v][0];
    dp[u][0] += max(dp[v][0], dp[v][1]);
  }
  return dp[u][0];
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) {
    int f;
    cin >> w[i] >> f;
    ch[f].push_back(i);
  }
  rep(i, 1, n) {
    if (!vis[i]) {
      r1 = r2 = 0;
      find(i, i);
      if (r1) {
        ll x = dfs(r1, r1);
        ll y = dfs(r2, r2);
        ans += max(x, y);
      }
    }
  }
  cout << ans;
  return 0;
}